using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GraphAPI.Model;
using GraphAPI;

namespace GraphEditor
{
	public partial class PropertiesNode : UserControl
	{
		public PropertiesNode()
		{
			InitializeComponent();
		}

        Vertex vertex;

        public void SetVertex(Vertex vertex)
        {
			this.vertex = vertex;

			txtName.Text = vertex.Text;
			cmbTextAlign.Text = vertex.TextAlign.ToString();

			if (vertex.Weight.Equals(double.NaN))
				txtWeigth.Text = "";
			else
				txtWeigth.Text = vertex.Weight.ToString();
			txtWeigth.BackColor = Color.White;

			txtCommentary.Text = vertex.Commentary;
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
				vertex.Text = txtName.Text;
                txtName.SelectAll();
            }
        }

        private void txtName_Leave(object sender, EventArgs e)
        {
			vertex.Text = txtName.Text;
        }

        private void cmbTextAlign_TextChanged(object sender, EventArgs e)
        {
            string text = cmbTextAlign.Text as string;
            if (text == null)
                return;

            TextAlign align;
            try
            {
                align = (TextAlign)Enum.Parse(typeof(TextAlign), text);
				vertex.TextAlign = align;
            }
            catch
            {
                return;
            }
        }

        private void ReWeigth(string weight)
        {
            double res;

			if (txtWeigth.Text.Length == 0)
			{
				vertex.Weight = double.NaN;
				return;
			}

			try
            {
                res = double.Parse(weight);
				vertex.Weight = res;
				txtWeigth.SelectAll();
				txtWeigth.BackColor = Color.White;
            }
            catch 
            {
				txtWeigth.BackColor = Color.Pink;
            }
        }

        private void txtWeigth_Leave(object sender, EventArgs e)
        {
            ReWeigth(txtWeigth.Text);
        }

        private void txtWeigth_KeyDown(object sender, KeyEventArgs e)
        {
			if (e.KeyCode == Keys.Escape)
			{
				txtWeigth.Text = vertex.Weight.ToString();
				return;
			}

            if (e.KeyCode == Keys.Enter)
                ReWeigth(txtWeigth.Text);
        }

        private void txtCommentary_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
				txtCommentary.Text = vertex.Commentary;
                return;
            }

            if (e.KeyCode != Keys.Enter)
                return;

            vertex.Commentary = txtCommentary.Text;
        }
	}
}

















